package ks.server.controllers.requests;

import java.util.Collection;

import org.w3c.dom.Document;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.managers.table.Table;

public class TableInfoRequest {
	private Communicator com;
	
	public TableInfoRequest(Communicator c) {
		this.com = c;
	}
	
	public boolean send(Collection<Table> tables) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<table-info>");
		for (Table table : tables) {
			sb.append("<table id=\"" + table.getId() + "\" time=\"\" plugin=\"\" undo=\"\" newhand=\"\" moderator=\"" + table.getModerator() + "\">");
			for (String player : table.getPlayers())
				sb.append("<playername name=\"" + player + "\"/>");
			sb.append("</table>");
		}
		sb.append("</table-info>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		r.setBroadcast();
		return com.distribute(r);
	}
	
	public boolean send(Collection<Table> tables, Collection<String> players) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<table-info>");
		for (Table table : tables) {
			sb.append("<table id=\"" + table.getId() + "\" time=\"\" plugin=\"\" undo=\"\" newhand=\"\" moderator=\"" + table.getModerator() + "\">");
			for (String player : table.getPlayers())
				sb.append("<playername name=\"" + player + "\"/>");
			sb.append("</table>");
		}
		sb.append("</table-info>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		return com.distribute(players.iterator(), r);
	}
}